package com.xueyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.ProductDao;
import com.xueyu.dao.ProductsDao;
import com.xueyu.domain.Products.ProductsBean;
import com.xueyu.domain.goods.Product;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/Products")
public class ProductsController {
    @Autowired
    ProductsDao productsDao;
    @Autowired
    ProductDao productDao;
    @RequestMapping("/add")
    public Map<String, Object> add(@RequestBody ProductsBean ProductsBean) throws Exception {
        Map<String, Object> map = new HashMap<>();

        String productName = ProductsBean.getProductName();
        int productId = ProductsBean.getProductId();
        QueryWrapper<ProductsBean> wrapper = new QueryWrapper<>();
        wrapper.eq("productName", productName);

        if (productId == 0) {
            int count = Math.toIntExact(productsDao.selectCount(wrapper));
            if (count > 0) {
                map.put("code", 201);
                map.put("msg", "请不要添重复的分类！");
                return map;
            } else {
                int result = productsDao.insert(ProductsBean);
                if (result != 0) {
                    map.put("code", 200);
                    System.out.println("true row" + result);
                } else {
                    System.out.println("Error");
                }
            }
        }else {
            ProductsBean oldBean = productsDao.selectById(productId);
            if (oldBean == null) {
                map.put("code", 201);
                map.put("msg", "分类不存在！");
            } else {
                int count = Math.toIntExact(productsDao.selectCount(wrapper));
                if (count > 0 && !productName.equals(oldBean.getProductName())) {
                    map.put("code", 201);
                    map.put("msg", "请不要添重复的分类！");
                } else {
                    int result = productsDao.updateById(ProductsBean);
                    if (result != 0) {
                        map.put("code", 200);
                        System.out.println("true");
                    } else {
                        System.out.println("false");
                    }
                }
            }
        }
        return map;
    }


    @RequestMapping("/del/{productId}")
    public Map<String, Object> del(@PathVariable("productId") int productId) {
        Map map = new HashMap();
        // 查询product表中是否存在与productId相同的style值
        QueryWrapper<Product> productQueryWrapper = Wrappers.query();
        productQueryWrapper.eq("style", String.valueOf(productId));
        List<Product> productList2 = productDao.selectList(productQueryWrapper);
        if (!productList2.isEmpty()) {
            map.put("code" ,201);
            map.put("msg", "存在货物信息绑定该分类，无法删除！");
        }else {
            productsDao.deleteById(productId);
            map.put("code", 200);
        }
        return map;
    }
    @RequestMapping("/getid/{productId}")
    public Map<String, Object> getid(@PathVariable("productId") int productId){
        ResultBean resultBean = new ResultBean();
        if (productId>0){
            QueryWrapper<ProductsBean> queryWrapper = Wrappers.query();
            queryWrapper.eq("productId", productId);
            List<ProductsBean> ProductsList = productsDao.selectList(queryWrapper);
            resultBean.put("data",ProductsList);
        }
        return resultBean;
    }
    @RequestMapping("/fy")
    public ResultBean fy(@RequestBody Map map){
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<ProductsBean> page = new Page<>(current, size);
        QueryWrapper<ProductsBean> queryWrapper = createSchoolQueryWrapper(map);
        page = productsDao.selectPage(page,queryWrapper);
        List<ProductsBean> list = page.getRecords();
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }
    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody Map map){
        QueryWrapper<ProductsBean> queryWrapper = createSchoolQueryWrapper(map);
        List<ProductsBean> list = productsDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
    private QueryWrapper<ProductsBean> createSchoolQueryWrapper(Map map) {
        String productId = (String) map.get("productId");
        String productName = (String) map.get("productName");
        QueryWrapper<ProductsBean> queryWrapper = Wrappers.query();
        if (productId != null && !"".equals(productId)) {
            queryWrapper.eq("productId", productId);
        }
        if (productName != null && !"".equals(productName)) {
            queryWrapper.like("productName", "%" + productName + "%");
        }
        return queryWrapper;
    }
}
